;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.MissingReport", [])
        .controller("MissingReportCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            // 调度员分页信息
            $scope.dispatchPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            // 代理商分页信息
            $scope.agentPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            // 订单类型分页信息
            $scope.orderPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            // 采购组分页信息
            $scope.purchasePageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            // 标签页信息
            $scope.activeFlag = [true, false, false, false];

            $scope.dispatchTb = {
                scrollWidth: 300,
                tableCol: [
                    {
                        name: "调度员名称",
                        dataField: "name",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "次数",
                        dataField: "count",
                        width: 150,
                        isShow: true,
                    }
                ]
            };

            $scope.agentTb = {
                scrollWidth: 300,
                tableCol: [
                    {
                        name: "代理商名称",
                        dataField: "name",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "次数",
                        dataField: "count",
                        width: 150,
                        isShow: true,
                    }
                ]
            };

            $scope.orderTb = {
                scrollWidth: 300,
                tableCol: [
                    {
                        name: "订单类型",
                        dataField: "name",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "次数",
                        dataField: "count",
                        width: 150,
                        isShow: true,
                    }
                ]
            };

            $scope.purchaseTb = {
                scrollWidth: 300,
                tableCol: [
                    {
                        name: "采购组",
                        dataField: "name",
                        width: 150,
                        isShow: true,
                        isFixed: true
                    },
                    {
                        name: "次数",
                        dataField: "count",
                        width: 150,
                        isShow: true,
                    }
                ]
            };

            $scope.exportParam = {
                dispatchUrl: ybHttp.RequestUrl("device", "/report/exportMissingDispatcher"),
                agentUrl: ybHttp.RequestUrl("device", "/report/exportMissingAgent"),
                orderUrl: ybHttp.RequestUrl("device", "/report/exportMissingOrderType"),
                purchaseUrl: ybHttp.RequestUrl("device", "/report/exportMissingMaterialOwnership"),
            };

            // 搜索条件
            $scope.dispatchCondition = {};
            $scope.agentCondition = {};
            $scope.orderCondition = {};
            $scope.purchaseCondition = {};

            $scope.dispatchList = [];
            $scope.agentList = [];
            $scope.orderList = [];
            $scope.purchaseList = [];

            // 按钮权限code
            var btnPermissionCode = {
                purchaseAmountAnalysisExport: window.ActionCode.purchaseAmountAnalysisExport
            };

            // 按钮权限
            $scope.btnPermission = {
                purchaseAmountAnalysisExport: true
            };

            // 设置按钮权限
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.changeTab = function (index) {
                angular.forEach($scope.activeFlag, function (item) {
                    item = false;
                });
                $scope.activeFlag[index] = true;
            };

            $scope.searchDispatch = function (curPage) {
                getDispatchList(curPage);
            };

            $scope.searchAgent = function (curPage) {
                getAgentList(curPage);
            };

            $scope.searchOrder = function (curPage) {
                getOrderList(curPage);
            };

            $scope.searchPurchase = function (curPage) {
                getPurchaseList(curPage);
            };

            // 获取调度员报表列表
            function getDispatchList (curPage) {
                var url = '/report/missingDispatcher';
                if (curPage) $scope.dispatchPageInfo.curPage = curPage;
                var params = {
                    filters: angular.copy($scope.dispatchCondition),
                    pageSize: $scope.dispatchPageInfo.pageSize,
                    pageNum: $scope.dispatchPageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.dispatchPageInfo.totalRows = res.value.total;
                    $scope.dispatchPageInfo.totalPages = res.value.pages;
                    $scope.dispatchList = res.value.list;
                })
            }

            // 获取代理商报表列表
            function getAgentList (curPage) {
                var url = '/report/missingAgent';
                if (curPage) $scope.agentPageInfo.curPage = curPage;
                var params = {
                    filters: angular.copy($scope.agentCondition),
                    pageSize: $scope.agentPageInfo.pageSize,
                    pageNum: $scope.agentPageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.agentPageInfo.totalRows = res.value.total;
                    $scope.agentPageInfo.totalPages = res.value.pages;
                    $scope.agentList = res.value.list;
                })
            }

            // 获取订单类型报表列表
            function getOrderList (curPage) {
                var url = '/report/missingOrderType';
                if (curPage) $scope.orderPageInfo.curPage = curPage;
                var params = {
                    filters: angular.copy($scope.orderCondition),
                    pageSize: $scope.orderPageInfo.pageSize,
                    pageNum: $scope.orderPageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.orderPageInfo.totalRows = res.value.total;
                    $scope.orderPageInfo.totalPages = res.value.pages;
                    $scope.orderList = res.value.list;
                })
            }

            // 获取采购组报表列表
            function getPurchaseList (curPage) {
                var url = '/report/missingMaterialOwnership';
                if (curPage) $scope.purchasePageInfo.curPage = curPage;
                var params = {
                    filters: angular.copy($scope.purchaseCondition),
                    pageSize: $scope.purchasePageInfo.pageSize,
                    pageNum: $scope.purchasePageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.purchasePageInfo.totalRows = res.value.total;
                    $scope.purchasePageInfo.totalPages = res.value.pages;
                    $scope.purchaseList = res.value.list;
                })
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })()
        }])
    ;
})(window, window.angular);